package com.data.structure.stack;

import lombok.extern.slf4j.Slf4j;

import java.util.Scanner;

/**
 * 链表模拟栈
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/12/30
 */
public class LinkedStackDemo {
    public static void main(String[] args) {
        LinkedStack stack = new LinkedStack();
        String key = "";
        boolean loop = true;
        Scanner scanner = new Scanner(System.in);

        while(loop) {
            System.out.println("show: 表示显示栈");
            System.out.println("exit: 退出程序");
            System.out.println("push: 表示添加数据到栈(入栈)");
            System.out.println("pop: 表示从栈取出数据(出栈)");
            System.out.println("请输入你的选择");
            key = scanner.next();
            switch(key.hashCode()) {
                case 111185:
                    if (key.equals("pop")) {
                        try {
                            int res = stack.pop();
                            System.out.printf("出栈的数据是 %d\n", res);
                        } catch (Exception var8) {
                            System.out.println(var8.getMessage());
                        }
                    }
                    break;
                case 3127582:
                    if (key.equals("exit")) {
                        scanner.close();
                        loop = false;
                    }
                    break;
                case 3452698:
                    if (key.equals("push")) {
                        System.out.println("请输入一个数");
                        int value = scanner.nextInt();
                        stack.push(value);
                    }
                    break;
                case 3529469:
                    if (key.equals("show")) {
                        stack.list();
                    }
            }
        }

        System.out.println("程序退出~~~");
    }
}

/**
 * 链表模拟栈
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/12/30
 */
@Slf4j
class LinkedStack{
    /**
     * 头结点
     */
    Node head = new Node(-1);
    /**
     * 栈顶
     */
    Node top = head;


    /**
     * 栈空
     * @return
     */
    public boolean isEmpty(){
        return top == head;
    }
    /**
     * 压栈
     * @param value
     */
    public void push(int value){
        Node node = new Node(value);
        top.next = node;
        node.pre = top;
        top =node;
    }
    /**
     * 出栈
     * @return
     */
    public int pop(){
        if(isEmpty()){
            log.info("【栈空】");
            throw new RuntimeException("栈空");
        }
        int value = top.value;
        top = top.pre;
        top.next.pre = null;
        top.next =null;
        return value;
    }
    public void list(){
        if(isEmpty()){
            log.info("【栈空】");
            return;
        }
        Node node = top;
        for(;;){

            log.info("【{}】",node.value);
            node = node.pre;
            if(head == node ){
                break;
            }
        }
    }

}
/**
 * 链表节点
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/12/30
 */
class Node{
    /**
     * 值
     */
    int value;
    /**
     * 下一个节点
     */
    Node next;
    /**
     * 上一个节点
     */
    Node pre;

    Node(int value){
        this.value = value;
    }

}
